package com.besall.allbase.view.activity.chipstoollevel4.protocol.InfoParams;

import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.GENDER_CHECK;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;


import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants;

public class PersonInfo extends DialogFragment {

    public EditText mHeight;
    public EditText mWeight;
    public EditText mStride_length;
    public EditText mRun_stride_length;
    public EditText mAge;
    public String mGender = "1";

    private ConfigCallBack mConfigCallback;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_personinfo, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        mHeight = (EditText) view.findViewById(R.id.height);
        mWeight = (EditText) view.findViewById(R.id.weight);
        mStride_length = (EditText) view.findViewById(R.id.stride);
        mRun_stride_length = (EditText) view.findViewById(R.id.run);
        mAge = (EditText) view.findViewById(R.id.age);

        RadioGroup chooseGender = (RadioGroup) view.findViewById(R.id.gender);
        chooseGender.setOnCheckedChangeListener(GenderCheckChangeListener);

        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }

    public boolean saveConfig(){
        String height = mHeight.getText().toString();
        if (TextUtils.isEmpty(height)) {
            Toast.makeText(getActivity(), "height can not be empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_HEIGHT_VALUE, height);

        String weight =mWeight.getText().toString();
        if (TextUtils.isEmpty(weight)) {
            Toast.makeText(getActivity(), "weight can not be empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_WEIGHT_VALUE, weight);

        String stride =mStride_length.getText().toString();
        if (TextUtils.isEmpty(stride)) {
            Toast.makeText(getActivity(), "stride can not be empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_STRIDE_VALUE, stride);

        String runStride =mRun_stride_length.getText().toString();
        if (TextUtils.isEmpty(runStride)) {
            Toast.makeText(getActivity(), "runStride can not be empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_RUN_STRIDE_VALUE, runStride);

        String age =mAge.getText().toString();
        if (TextUtils.isEmpty(age)) {
            Toast.makeText(getActivity(), "age can not be empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_AGE_VALUE, age);
        SPHelper.putPreference(getActivity(), GENDER_CHECK, mGender);
        return true;
    }

    private final RadioGroup.OnCheckedChangeListener GenderCheckChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {


            if(checkedId == R.id.genderm){

                mGender = "0";
            }else if(checkedId == R.id.genderf){

                mGender = "1";
            }

 /*           switch (checkedId){
                case R.id.genderm:
                    mGender = "0";
                    break;
                case R.id.genderf:
                    mGender = "1";
                    break;
            }*/
        }
    };

    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
